package ch.lom.clemens.android.bibliography;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import ch.lom.clemens.android.bibliography.data.MCollection;
import ch.lom.clemens.android.bibliography.data.MDocument;

public class DocumentsActivity extends ListActivity {
	ArrayList<HashMap<String,Object>> m_list = new ArrayList<HashMap<String,Object>>();	
	SimpleAdapter m_adapter;

	public  static MCollection m_newCol;
	private static MCollection m_curCol = null;
	
	public static DocumentsActivity singleton; 
    
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        singleton = this;

        if (m_list.isEmpty()) {
	        HashMap<String,Object> item = new HashMap<String,Object>();
	        item.put("line1", "Empty");
	        item.put("line2", "Please select collection in from its tab");
	        m_list.add(item);
        }
        
		m_adapter = new SimpleAdapter(this, m_list, R.layout.documentsitem, new String[]{"line1","line2"}, new int[]{R.id.bt_text1, R.id.bt_text2});
		setListAdapter(m_adapter);
    }
    
    protected void onListItemClick(ListView lv, View v, int position, long id) {
		@SuppressWarnings("unchecked")
		HashMap<String,Object> item = (HashMap<String, Object>)m_adapter.getItem(position);
		
		DocumentActivity.m_curDoc = (MDocument) item.get("MDocument");
		
		StartActivity.singleton.getTabHost().setCurrentTabByTag(StartActivity.TAB_DOCUMENT);
		
	}
    
    public void setCollection(String colId) {

		
    }
    
    private final Handler  m_handler = new Handler();
    private final Runnable m_updateListDone = new Runnable() {
    	public void run() {
    		getParent().setProgressBarVisibility(false);
    		getParent().setProgressBarIndeterminateVisibility(false);
    		if(m_curCol!=null) getParent().setTitle(m_curCol.name);
    		m_adapter.notifyDataSetChanged();
    	}
    };
    
    class SetCollectionThread extends Thread {
    	MCollection m_col;
		public boolean m_stop;
		
    	public SetCollectionThread(MCollection col) {
    		m_col = col;
    		m_stop = false;
    	}
    	public void run() {
        	try {
        		m_list.clear();

        		Collection<MDocument> docs = m_col.getDocuments();
        		
        		if (docs == null || docs.size() == 0) {
    				HashMap<String,Object> item = new HashMap<String,Object>();
    				item.put("line1", "No items available");
    				item.put("line2", "");
    				m_list.add(item);
    				m_handler.post(m_updateListDone);
    				return;
        		}
        		
        		int i=0;
        		for (MDocument document : docs) {
        			
    				StartActivity.singleton.setProgress( i*10000 / docs.size());
        			i++;
        			
    				HashMap<String,Object> item = new HashMap<String,Object>();
    				item.put("line1", document.getTitle());

    				String strAuthors = document.getAuthorsString();
    				
    				item.put("line2", strAuthors);
    				item.put("MDocument", document);
        			
    				m_list.add(item);
        		}

        		// only set done if all documents have been loaded successfully
        		m_curCol = m_newCol;
    			
    		} catch (Exception e) {
    			Log.d("DocumentsActivity","Exception on setCollection: " + e.getMessage());
    		}
    		m_handler.post(m_updateListDone);
    	}
    };
    private static SetCollectionThread m_updateList;
    
    
    public void onResume() {
    	super.onResume();

    	if ( m_curCol != m_newCol ) {
    		
    		getParent().setProgress(0);
    		getParent().setProgressBarVisibility(true);
    		getParent().setProgressBarIndeterminateVisibility(true);
    		getParent().setTitle("Loading...");
    		m_list.clear();
    		m_adapter.notifyDataSetChanged();
    		if (m_updateList != null) {
    			m_updateList.m_stop = true;
    		}
    		m_updateList = new SetCollectionThread(m_newCol);
    		m_updateList.start();
    	} else if (m_curCol != null) {
    		getParent().setTitle(m_curCol.name);
    	}
    }
    
    
}
